//package com.sql.service;
//
//import lombok.RequiredArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.boot.CommandLineRunner;
//import org.springframework.jdbc.core.JdbcTemplate;
//import org.springframework.stereotype.Service;
//
//import java.io.File;
//import java.io.IOException;
//import java.nio.file.Files;
//import java.util.ArrayList;
//import java.util.Arrays;
//import java.util.List;
//import java.util.concurrent.ExecutorService;
//import java.util.concurrent.Executors;
//import java.util.concurrent.atomic.AtomicBoolean;
//import java.util.concurrent.atomic.AtomicInteger;
//import java.util.stream.Stream;
//
//@Service
//@RequiredArgsConstructor
//@Slf4j
//public class SqlImportService implements CommandLineRunner {
//
//    private final JdbcTemplate jdbcTemplate;
//
//    private static final String filename = "overseas_";
//    private static final String path = "E:\\下载\\sq\\overseas_split\\";
//
//    private static final AtomicBoolean flag = new AtomicBoolean(true);
//
//    private static final AtomicInteger suffix_int = new AtomicInteger(0);
//    private final ExecutorService executorService = Executors.newFixedThreadPool(40);
//
//    public void importSqlFiles() {
//        for (int i = 0; i < 40; i++) {
//            log.info("线程开始执行。。。");
//            executorService.submit(() -> {
//                while (flag.get()) {
//                    String suffix = String.format("%04d", suffix_int.incrementAndGet());
//                    String fp = path + filename + suffix + ".sql";
//                    File file = new File(fp);
//                    if (!file.exists()) {
//                        flag.set(false);
//                    }
//                    executeSqlFile(file);
//                    file.delete();
//                }
//            });
//        }
//    }
//
//    private void executeSqlFile(File sqlFile) {
//        try (Stream<String> lines = Files.lines(sqlFile.toPath())) {
//            List<String> batch = new ArrayList<>();
//            lines.forEach(line -> {
//                batch.add(line);
//                if (batch.size() >= 50) { // 假设每次执行50条语句
//                    String[] strings = batch.toArray(new String[0]);
//                    log.info(Arrays.toString(strings));
//                    jdbcTemplate.batchUpdate(strings);
//                    batch.clear();
//                }
//            });
//            if (!batch.isEmpty()) {
//                jdbcTemplate.batchUpdate(batch.toArray(new String[0]));
//            }
//        } catch (IOException e) {
//            // 这里应该处理异常，记录日志
//        }
//    }
//
//    @Override
//    public void run(String... args) throws Exception {
//        importSqlFiles();
//    }
//}
